<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ads extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mads");
        $this->load->model("mresource");
		
		$this->resCode = $this->mads->resCode;
        $this->url_index = admin_url() . "ads";
        $this->folder_upload = "dataupload/ads/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index($filter = '') {
        $data = array();
        
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['filter'] = $filter;
		
		$condition = array();
		if($filter != ''){
			if($filter =='index'){
				$condition['ADS_CODE']= 'trang-chu';
			}
			else if($filter =='category'){
				$condition['ADS_CODE']= 'category';
			}
			else {
			$condition['ADS_CODE']= 'detail';
			}
			$data['datas'] = $this->mads->gets($condition);
		}
		else {
			$data['datas'] = $this->mads->gets();
		}
        $this->theme_data["main"] = "ads/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($manID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['ads'] = $this->mads->get($manID, 'ADS_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['ads']->OS_THUMB)){
				$post['ads']['OS_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($manID > 0) {
                $this->mads->update($manID, 'ADS_ID', $post['ads']);
            }
			else{
				$manID = $this->mads->insert($post['ads']);
			}
            redirect($this->url_edit . $manID);
        }

        $this->theme_data["main"] = "ads/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($manID = 0){
		if($manID > 0){
			$info = $this->mos->get($manID, 'ADS_ID');
			if(file_exists('./'.$info->OS_THUMB))
				unlink('./'.$info->OS_THUMB);
			$this->mos->delete($manID, 'ADS_ID');
		}
	}
	
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
}
